package com.wtpaint.algorithm.sort;

import com.wtpaint.algorithm.SortUtil;
import com.wtpaint.array.Array;

import java.util.Arrays;

public class InsertionSort {
    private InsertionSort() {
    }

    public static void sort(Comparable[] arr) {
        int n = arr.length;
        for (int i = 0; i < n; i++) {

            for (int j = i; j > 0; j--) {
                if (arr[j].compareTo(arr[j - 1]) < 0) {
                    swap(arr, j, j - 1);
                } else {
                    break;
                }
            }
        }
    }

    private static void swap(Object[] arr, int i, int j) {
        Object t = arr[i];
        arr[i] = arr[j];
        arr[j] = t;
    }


    public static void main(String[] args) {
        int n = 10000;
        Integer[] arr1 = SortUtil.generateRandomArray(n, 0, n);
        Integer[] arr2 = Arrays.copyOf(arr1, arr1.length);
        SortUtil.testSort("com.wtpaint.algorithm.sort.SelectionSort", arr1);
        SortUtil.testSort("com.wtpaint.algorithm.sort.InsertionSort", arr2);
    }
}
